package Model
{
	import flash.events.Event;
	
	import mx.events.PropertyChangeEvent;
	
	import Controller.AreasController;

	public class HeatedSpaceArea extends SimpleComponent
	{
		private var _areaType:String;
		private var _livingArea:Number;
		private var _estimation:Number;
		private var _area:Number;
		private var areaController:AreasController;
		private var propertyChangedEvent:PropertyChangeEvent = new PropertyChangeEvent("propertyChanged");
		public static var counter:Number = 1;
		
		public function HeatedSpaceArea(description:String="", quantity:Number=1, livingArea:Number = 0, estimation:Number = 0, area:Number=0, areaType:String = "Brut")
		{
			this.areaController = new AreasController();
			
			super(description, quantity);
			this.id = generateId();
			this._areaType = areaType;
			this._area = area;
			this._estimation = estimation;
			this._livingArea = livingArea;
			this.addEventListener("internalChange", handleInternalChange);
		}
		
		override public function generateId():String
		{
			var result:String = "";
			
			if(counter < 9)
			{	
				result = "LA0" + counter.toString();
			}
			else
			{
				result = "LA" + counter.toString();
			}
			
			counter++;
			
			return result;
		}

		public function get areaType():String
		{
			return _areaType;
		}

		public function set areaType(value:String):void
		{
			_areaType = value;
		}
		
		public function get estimation():Number
		{
			return _estimation;
		}

		public function set estimation(value:Number):void
		{
			_estimation = value;
			this.dispatchEvent(this.internalChangeEvent);
		}

		public function get livingArea():Number
		{
			return _livingArea;
		}

		public function set livingArea(value:Number):void
		{
			_livingArea = value;
			this.dispatchEvent(this.internalChangeEvent);
		}
		
		public function get area():Number
		{
			return _area;
		}
		
		public function set area(value:Number):void
		{
			_area = value;
			this.propertyChangedEvent.property = this.area;
			this.dispatchEvent(this.propertyChangedEvent);
		}
		
		public function calculateHeatedSpaceArea():void
		{
			this.area = this.quantity *(this.livingArea + this.estimation);
		}
		
		protected function handleInternalChange(event:Event):void
		{
			this.calculateHeatedSpaceArea();
			this.areaController.updateAreaOverviewStatus();
		}

	}
}